// pages/xinx_show/xinx_show.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //收藏
    collection: false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var userInfo = wx.getStorageSync("userInfo");
    var id = options.id
    var type = options.type
    if (id && type){
      if (userInfo) {
        
        this.setData({
          user: userInfo,
          uid: userInfo.id,
          id: id,
          type: type
        })
        this.getshow(id, type)
      }
    }else{
      wx.showToast({
        title: "参数异常",
        icon: 'none',
        duration: 2000
      })
      setTimeout(function () {
        wx.navigateBack();
      }, 2000)
    }
    
      
  },

  //信息推荐
  getshow: function (id, type){
    let _this=this
    app.wxRequest('get','/Samecity/Ads/getshow', {id:id,type:type}, function (res) {
      console.log(res)
      _this.setData({
        data: res.data.data,
      })
    }, function (err) {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })
    })
  },
   /*收藏*/
  collection: function (e) {
    let that = this
    var id = e.currentTarget.dataset.value
    var uid = that.data.uid
    console.log(uid);
    var type = 1
    app.wxRequest('get','/Samecity/Ads/getsc', {id:id,type:type,uid:uid}, function (res) {
      if(res.data.code==1){
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        that.setData({
          collection:true
        })
       
      }else if(res.data.code==2){
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        that.setData({
          collection : false
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
   
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMaterial();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //一键导航地图
  btn_map: function (e) {
  let _this = this;
    const latitude = parseFloat(_this.data.data.latitude)
    const longitude = parseFloat(_this.data.data.longitude)
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 18,
      title: "简介",
      name: '标题',
      address: '详细地址'
    })
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

 

  /*拨号*/
  calling: function () {
    let _this=this
    let that = this
    var key = e.currentTarget.dataset['index'];
    wx.showActionSheet({
      itemList: [key],
     
      success: function (res) {
        console.log(res) //当点击400-900-2250就相当于点击了
        wx.makePhoneCall({
          phoneNumber: tel, //此号码并非真实电话号码，仅用于测试  
          success: function () {
            console.log("拨打电话成功！")
          },
          fail: function () {
            console.log("拨打电话失败！")
          }
        })
        if (!res.cancel) {
          console.log(res.tapIndex)//console出了下标
        }
      }
    });
  },

  //信息推荐
  getMaterial:function(){
    let _this=this
    var city = wx.getStorageSync("city")
    app.wxRequest('get','/Samecity/Ads/getMaterial', {city:city}, function (res) {
      console.log(res)
      _this.setData({
        MaterialList: res.data.data,
      })
    }, function (err) {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })
    })
  }




})